import { Component, OnInit, ViewChild } from '@angular/core';
import{ActivatedRoute} from  '@angular/router';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { environment } from '@env/environment';

@Component({
  selector: 'app-simple-list',
  templateUrl: './list.component.html',
})
export class SimpleListComponent implements OnInit {
  isDataLoad = false
  code = "";
  url = '';
  searchSchema: SFSchema = {properties: {}};

  @ViewChild('st', { static: false }) st!: STComponent;
  columns: STColumn[] = [];

  constructor(private http: _HttpClient, private modal: ModalHelper, private route:ActivatedRoute) { }

  ngOnInit() { 
    
    this.code = this.route.snapshot.params.code;
    this.url = `${environment.simple.ctx}/simple/list/data/${this.code}`
    this.searchSchema.properties!.name = {
      type: 'string',
      title: '名称'
    }
    
    this.http.get(`${environment.simple.ctx}/simple/list/${this.code}`).subscribe(res =>{
      this.setSearchSchema(res);
      this.setColumns(res);
      this.isDataLoad = true;
    })
  }

  add() {
    // this.modal
    //   .createStatic(FormEditComponent, { i: { id: 0 } })
    //   .subscribe(() => this.st.reload());
  }

  setSearchSchema(schema: any){
    
    schema.searchSchema.forEach((item: any)=>{
      this.searchSchema.properties![item.key] = {
        type: item.type,
        title: item.title
      }
    })
  }

  setColumns(schema: any){
    schema.columns.forEach((item: any)=>{
      this.columns.push({ title: item.title, index: item.index });
    })
  }

}
